#include<iostream>
using namespace std;
#include"vector.h"
#include<list>
#include<string>

void print_vector(const fym::vector<int>& v)
{
    // typename fym::vector<T>::const_iterator it = v.begin();
    fym::vector<int>::const_iterator it = v.begin();
    while (it != v.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
}
void test_vector1()
{
    // 测试push_back,size(),capacity(),reserve(),operator[]等
    fym::vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    for (int i = 0; i < v.size(); i++)
    {
        cout << v[i] << " ";
    }
    cout << endl;

    // 测试迭代器
    fym::vector<int>::iterator it = v.begin();
    while (it != v.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl << endl;

    // 测试const_iterator和const_iterator begin()和const_iterator end()
    print_vector(v);
    cout << endl;

    // pop_back
    for (int i = v.size() - 1; i >= 0; i--)
    {
        cout << v[i] << " ";
        v.pop_back();
    }
    cout << endl << v.size() << endl;
    //v.pop_back();// v删空再删断言报错
}

void test_vector2()
{
    // 测试insert
    fym::vector<int> v;
    // 头插
    v.insert(v.begin(), 3);
    v.insert(v.begin(), 2);
    v.insert(v.begin(), 1);
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
    // 尾插
    v.insert(v.end(), 4);
    v.insert(v.end(), 5);
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
    // 中间
    v.insert(v.end() - 2, 0);
    fym::vector<int>::iterator it = v.insert(v.begin() + 2, 0);
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl << endl;
    cout << *(it - 1) << " " << *it << " " << *(it + 1) << endl << endl;
    auto pos = find(v.begin(), v.end(), 3);
    if (pos != v.end())
        cout << *(pos - 1) << " " << *pos << " " << *(pos + 1) << endl << endl;

    // 测试erase，同时注意insert与erase导致的迭代器失效问题
    cout << "-----------------------" << endl;
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl << endl;
    it = v.begin();
    // 删除偶数
    while (it != v.end())
    {
        if (*it % 2 == 0) it = v.erase(it);
        else ++it;
    }
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl << endl;
}
void test_vector3()
{
    // 测试resize
    fym::vector<int> v;
    v.resize(10, 1);
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl << endl;
    v.resize(15, 2);
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl << endl;
    v.resize(0);
    cout << v.size() << endl << endl;
}
template<class Container>
void print_container(const Container& v)
{
    for (auto e : v)
    {
        cout << e << " ";
    }
    cout << endl;
}
void test_vector4()
{
    // vector构造与析构相关的深拷贝问题测试
    fym::vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    print_container(v);
    // 拷贝构造，默认构造测试
    fym::vector<int> v1 = v;
    print_container(v1);
    v1.push_back(5);
    print_container(v1);
    // 赋值测试
    fym::vector<int> v3;
    v3 = v1;
    print_container(v3);
    // 迭代器区间构造及模板测试
    cout << "------------------------------" << endl;
    fym::vector<int> v4(v3.begin(), v3.begin()+3);
    print_container(v4);
    cout << "------------------------------" << endl;
    list<int> lt;
    lt.push_back(10);
    lt.push_back(10);
    lt.push_back(10);
    lt.push_back(10);
    fym::vector<int> v5(lt.begin(), lt.end());
    print_container(v5);
    // 测试n个值初始化的构造
    cout << "------------------------------" << endl;
    fym::vector<string> v6(4, "abc");
    print_container(v6);

    // 这里需要注意模板的实例化原则：寻找最匹配的
    //vector(size_t n, const T& val = T())
    //{
    //    reserve(n);
    //    for (int i = 0; i < n; i++) push_back(val);
    //}
    //// 类模板的成员函数还可以是函数模板
    //template<class InputIterator>
    //vector(InputIterator first, InputIterator last)
    //{
    //    while (first != last)
    //    {
    //        push_back(*first);
    //        ++first;
    //    }
    //}
    // 这里想调用第一个模板用10个1初始化v，但两个int形参显然更匹配下面的迭代器构造函数模板，导致调用错误
    // 解决办法一：fym::vector<int> v(10u, 1);  10后面加个u，强转成size_t类型
    // 解决办法二：在vector类模板中特别实现一个类型匹配的vector(int n, const T& val = T())
    //fym::vector<int> v(10, 1);
}
int main()
{
	//test_vector1();
    //test_vector2();
    //test_vector3();
    test_vector4();
	return 0;
}
